package com.example.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.article.mapper.ArticleMapper;
import com.example.common.util.R;
import com.example.common.util.ResultCodeRecord;
import com.example.model.article.dto.ArticleCommentDto;
import com.example.model.article.dto.ArticleDto;
import com.example.model.article.pojo.Article;
import com.example.model.article.pojo.ArticleConfig;
import com.example.model.article.vo.ArticleCommnetVo;
import com.example.model.wemedia.dto.StatisticsDto;
import com.example.utils.common.DateUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.article.service.ArticleService;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/")
@Tag(name = "文章信息管理")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping(value = "/article/queryLikesAndConllections")
    @Operation(summary = "查询喜欢文章和选择收藏文章")
    public R queryLikesAndConllections(@RequestParam(value = "userId") Integer userId, @RequestParam(value = "beginDate") Date beginDate, @RequestParam(value = "endDate") Date endDate)  {
        Object object = articleService.queryLikesAndConllections(userId, beginDate, endDate);
        if (object != null) {
            return R.ok("查询喜欢文章和选择收藏文章成功").data("likesList", object);
        } else {
            return R.error("查询喜欢文章和选择收藏文章失败");
        }
    }

    @PostMapping(value = "/article/getNewPage/{page}/{pageSize}")
    @Operation(summary = "模糊查询文章分页")
    public R getNewPage(@RequestBody StatisticsDto statisticsDto, @PathVariable(value = "page") Long page, @PathVariable(value = "pageSize") Long pageSize)  {
        // 类型转换
        Date beginDate = DateUtils.stringToDate(statisticsDto.getBeginDate());
        Date endDate = DateUtils.stringToDate(statisticsDto.getEndDate());
        // 1、检查参数是否有效
        if (statisticsDto == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        // 2、分页查询
        Page<Article> articlePage = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getAuthorId, statisticsDto.getUserId());
        wrapper.between(Article::getPublishTime, beginDate, endDate);
        wrapper.select(Article::getArticleId, Article::getArticleTitle, Article::getArticleLikes, Article::getArticleCollection, Article::getArticleComment, Article::getArticleViews);
        // 排序
        wrapper.orderByDesc(Article::getPublishTime);
        // 查询数据
        articleService.page(articlePage, wrapper);
        // 每页的数据集合
        List< Article > articleList = articlePage.getRecords();
        // 总的记录数
        Long total = articlePage.getTotal();
        // 每页条数
        Long size = articlePage.getSize();
        // 总页数
        Long pages = articlePage.getPages();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("pageSize", size);
        map.put("pages", pages);
        map.put("page", page);
        map.put("articleList", articleList);
        if (map != null) {
            return R.ok("模糊查询文章分页成功").data(map);
        } else {
            return R.error("模糊查询文章分页失败");
        }
    }

    @PostMapping(value = "/article/getNewsCommentsPage/{page}/{pageSize}")
    @Operation(summary = "查询文章评论分页")
    public R getNewsCommentsPage(@RequestBody ArticleCommentDto articleCommentDto, @PathVariable(value = "page") Long page, @PathVariable(value = "pageSize") Long pageSize)  {
        // 2、分页查询
        Page<Article> articleCommentPage = new Page<>(page, pageSize);
        // 每页的数据集合
        List< ArticleCommnetVo > articleCommnetVoList = articleMapper.getNewsCommentsPage(articleCommentDto, (page - 1) * pageSize, pageSize);
        // 总的记录数
        Long total = articleCommentPage.getTotal();
        // 每页条数
        Long size = articleCommentPage.getSize();
        // 总页数
        Long pages = articleCommentPage.getPages();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("pageSize", size);
        map.put("pages", pages);
        map.put("page", page);
        map.put("articleCommentList", articleCommnetVoList);
        if (map != null) {
            return R.ok("查询文章评论分页成功").data(map);
        } else {
            return R.error("查询文章评论分页失败");
        }
    }
}